package tex.parser;

import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import org.junit.Test;

import tex.base.Tex;

public class Parser {

	private void printCompilation(Tex tex) {
		StringBuffer buf = new StringBuffer();
		tex.generateDocument(buf);
		System.out.println(buf);
	}

	@Test
	public void ParserShouldParseMyMonographyWithoutError() throws Exception {
		Tex compile = compileFile("../../TeX/monografia.tex");
		assertNotNull(compile);
	}

	@Test
	public void ParserShouldParseCarniellysExampleWithoutError()
			throws Exception {
		Tex compile = compileFile("../../TeX/carnielli.tex");
		// printCompilation(compile);
		assertNotNull(compile);
	}

	@Test
	public void ParserShouldParseSeveralFilesWithoutError() throws Exception {
		String baseDir = "../../TeX/";
		String [] files = new String[]{"iwod2009.tex", "sbia.tex", "SvenOve-final.tex", "EBL.tex", "Onto2009.tex"};
		
		for (String filename : files) {
			String file = baseDir + filename;
			System.out.println("processing file: " + file);
			Tex compile = compileFile(file);
			assertNotNull(compile);
		}
	}

	private Tex compileFile(String filename) throws ParseException,
			FileNotFoundException {
		File file = new File(filename);
		FileReader reader = new FileReader(file);
		TexGrammar compiler = new TexGrammar(reader);
		Tex compile = compiler.compile();
		return compile;
	}
}
